﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using Utility.Json;
using Hxj.Data;
using System.Data;
using Utility.Comm;
using MsgCenter.DAL.Base;

namespace MsgCenter.BLL.Impl
{
    /// <summary>
    /// 公共数据调用接口
    /// </summary>
    public class BLL_BaseService
    {
        private readonly BaseService dal = new BaseService();
        /// <summary>
        /// 获取通用分页
        /// </summary>
        /// <typeparam name="TEntity">对象实体</typeparam>
        /// <param name="pager">PagerCondition对象</param>
        /// <returns></returns>
        public JsonJqGridData<TEntity> GetGridDataList<TEntity>(PagerCondition pager) where TEntity : Entity, new()
        {
            JsonJqGridData<TEntity> grid;
            grid.page = pager.CurrentPage;
            grid.rows = dal.GetList<TEntity>(pager);
            grid.records = pager.RecordCount;
            grid.total = pager.PageCount;
            return grid;
        }

        /// <summary>
        /// 获取通用分页
        /// </summary>
        /// <typeparam name="TEntity">对象实体</typeparam>
        /// <param name="pager">PagerCondition对象</param>
        /// <returns></returns>
        public JsonJqGridData GetGridDataTable<TEntity>(PagerCondition pager) where TEntity : Entity, new()
        {
            JsonJqGridData grid;
            grid.page = pager.CurrentPage;
            grid.rows = dal.GetTable<TEntity>(pager);
            grid.records = pager.RecordCount;
            grid.total = pager.PageCount;
            return grid;
        }

        /// <summary>
        /// 根据对象获取Table
        /// </summary>
        /// <typeparam name="TEntity">对象实体</typeparam>
        /// <param name="wcp">WhereClip条件</param>
        /// <returns></returns>
        public DataTable GetDataTable<TEntity>(WhereClip wcp, OrderByClip orderby = null) where TEntity : Entity
        {
            return dal.GetTable<TEntity>(wcp, orderby);
        }

        /// <summary>
        /// 获取对象实体
        /// </summary>
        /// <typeparam name="TEntity">对象实体</typeparam>
        /// <param name="wcp">WhereClip条件</param>
        /// <returns></returns>
        public TEntity GetModel<TEntity>(WhereClip wcp) where TEntity : Entity
        {
            return dal.GetModel<TEntity>(wcp);
        }

        /// <summary>
        /// 获取对象实体List
        /// </summary>
        /// <typeparam name="TEntity">对象实体</typeparam>
        /// <param name="wcp">WhereClip条件</param>
        /// <returns></returns>
        public List<TEntity> GetList<TEntity>(WhereClip wcp, OrderByClip orderby = null) where TEntity : Entity
        {
            return dal.GetList<TEntity>(wcp, orderby);
        }

        /// <summary>
        /// 获取第一行第一列数据
        /// </summary>
        /// <typeparam name="TEntity">TEntity实体对象</typeparam>
        /// <typeparam name="TResult">类型</typeparam>
        /// <param name="wcp">WhereClip条件</param>
        /// <returns>TResult类型</returns>
        public TResult ToScalar<TEntity, TResult>(WhereClip wcp) where TEntity : Entity
        {
            return dal.ToScalar<TEntity, TResult>(wcp);
        }

        /// <summary>
        /// 获取第一行第一列数据
        /// </summary>
        /// <typeparam name="TResult">类型</typeparam>
        /// <param name="strSql">SQL语句</param>
        /// <returns>TResult类型</returns>
        public TResult ToScalar<TResult>(string strSql)
        {
            return dal.ToScalar<TResult>(strSql);
        }

        /// <summary>
        /// 获取数据总数
        /// </summary>
        /// <typeparam name="TEntity">TEntity实体对象</typeparam>
        /// <param name="wcp">WhereClip条件</param>
        /// <returns>int类型</returns>
        public int ToCount<TEntity>(WhereClip wcp) where TEntity : Entity
        {
            return dal.ToCount<TEntity>(wcp);
        }
    }
}
